#coding:utf-8
import serial,time,re
from checkSum import hex_checksum
from str2arry import str2arry
print "打开串口",
t = serial.Serial(port='com4', baudrate=9600, timeout=5)

print t.portstr

def serialCom(sendData,Num=None):

    sendData=sendData.decode('hex')
    t.write(sendData)
    # if Num !=0:
    receiveDataSource=t.read(Num)  #原始接收数据

    receiveDataHex=receiveDataSource.encode('hex').upper()
    out = receiveDataHex

    # if receiveDataHex!=None:
    #     p1 = r'\w\w16FF'
    #     p2 = r'FE68\w*16FF'
    #     pattern1 = re.compile(p1)    #校验和模板
    #     pattern2 = re.compile(p2)    #数据模板
    #     checkSum = re.search(pattern1, receiveDataHex)
    #     print checkSum
    #     data = re.search(pattern2, receiveDataHex)
    #     print data
    #     if checkSum !=None and data !=None:
    #         checkSum=checkSum.group()[:2]
    #         print checkSum
    #         data = str2arry(data.group())[2:-6]
    #         print data
    #
    #         if checkSum.split()==hex_checksum(data):
    #             out= receiveDataHex
    #         else: out = '00'
    #     else :out = '00'

    # receiveDataArry=str2arry(receiveDataHex)
    time.sleep(3)
    return out





